package org.example.mybatisdemo.mapper;

import org.apache.ibatis.annotations.*;
import org.example.mybatisdemo.model.UserInfo;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    //复用结果映射 @ResultMap
    @ResultMap("resultMap")
    @Select("select * from user_info")
    List<UserInfo> getUserInfoAll();

    @Select("select * from user_info where delete_flag = #{deleteFlag}")
    List<UserInfo> getUserInfoByDeleteFlag(Integer flag);

    @Select("select * from user_info where delete_flag = ${deleteFlag}")
    List<UserInfo> getUserInfoByDeleteFlag2(Integer flag);

    @Select("select * from user_info where username = #{userName} ")
    UserInfo getUserByanme(String userName);

    @Select("select * from user_info where username = '${userName}' ")
    UserInfo getUserByanme2(String userName);

    @Select("select * from user_info where delete_flag = #{deleteFlag} and gender = #{gender}")
    List<UserInfo> getUserInfo2(@Param("deleteFlag") Integer deleteFlag, Integer gender);

    //如果有只有一个结果返回 可以使用集合或者对象接收
    //如果有多个结果返回，只能能使用集合接收
    @Select("select * from user_info where id = #{id}")
    UserInfo  getUserInfo3(Integer id);

    //获取主键
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info (username,password,age,gender) values(#{username},#{password},#{age},#{gender})")
    Integer insert(UserInfo userInfo);

    @Insert("insert into user_info (username,password,age,gender, ) "+
            "values(#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from user_info where id=#{id}")
    Integer delete(Integer id);

    @Update("update  user_info set password=#{password},age=#{age},gender=#{gender} where id= #{id}")
    Integer update(UserInfo userInfo);


    @Select("select id,username,password,age,gender,phone, "+
            "delete_flag as deleteFlag,create_time as createTime,update_time as updateTime from user_info")
    List<UserInfo> selectUserInfos();


    @Results(id="resultMap", value={
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("SELECT delete_flag,create_time,update_time from user_info" )
    List<UserInfo> selectUserInfos2();

    @Select("SELECT delete_flag,create_time,update_time from user_info" )
    List<UserInfo> selectUserInfos3();

    /**
     * 演示SQL注入
     */
    @Select("select * from user_info where username = #{userName}")
    UserInfo selectUserByName(String userName);

    @Select("select * from user_info where username = '${userName}'")
    List<UserInfo> selectUserByName2(String userName);

    @Select("select * from user_info where username= '${username}' and password = '${password}'")
    UserInfo queryUserByNameAndPassword(String username, String password);

    @Select("select * from user_info order by id ${order}")
    List<UserInfo> queryUserListByOrder(String order);
    @Select("select * from user_info order by id asc")
    List<UserInfo> queryUserListByOrder1 (String order);


    @Select("select * from user_info where username like CONCAT('%',#{username},'%')")//拼接‘%’的方式
    List<UserInfo> queryUserListByLike(String username);

    @Select("select * from user_info where username like '%${username}%'")//也可以实现 但是能不用则不用  防止SQL注入
    List<UserInfo> queryUserListByLike1(String username);

    @Insert("<script>" +
            "insert into user_info " +
            "<trim prefix='(' suffix=')' suffixOverrides=','> " +
            "    <if test='username!=null'>username,</if> " +
            "    <if test='password !=null'>password,</if> " +
            "    <if test='age !=null'>age, </if> " +
            "    <if test='gender != null'> gender</if> " +
            "</trim> " +
            " values " +
            "<trim prefix='(' suffix=')' suffixOverrides=','> " +
            "    <if test='username!=null'> #{username},</if> " +
            "    <if test='password !=null'>#{password},</if> " +
            "    <if test='age !=null'>#{age},</if> " +
            "    <if test='gender !=null'> #{gender}</if> " +
            "</trim>" +
            "</script>")
    Integer insertByCondtion(UserInfo userInfo);


}
